

package lists;

import basic.MainClass;
import exceptions.ObjectNotFound;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;


public abstract class GenericList <T extends MainClass> {
    
    // this class implemets all nessecery methods to use a Client, Bike, Reservation lists
    
    private static final Logger logger = Logger.getLogger( "lab2_generic.lists.GenericList" );
     
    protected List<T> main = new ArrayList<>();
    
    //*******************************************************************
    public List<T> getList(){
        return main;
    }
    
    //*******************************************************************
    /**
     * 
     * @param obj
     * @throws NullPointerException if obj paramter is null value 
     */
        public void add(T obj) throws NullPointerException {
        logger.entering(getClass().getSimpleName(), "add method");
        if ( obj!=null ){
            // type of obj is propriet - it is guaranteed by data checking
             logger.log(Level.SEVERE, "add method : size of list before {0}", main.size() );
             main.add(obj);  // should always return true
             logger.log(Level.SEVERE, "add method : size of list after {0}", main.size() );
             logger.exiting( getClass().getSimpleName(), "add method - added successfully" );
             
        }
        else{
            logger.exiting( getClass().getSimpleName() , "add method - parameter is null" );
            throw new NullPointerException();
        }
      
     }
    
    
     //*******************************************************************
     public T getObj(int id) throws ObjectNotFound {
         logger.entering(getClass().getSimpleName(), "getObj method");
         int z = ifExist(id);
         
         if (z>=0) {
             logger.exiting( getClass().getSimpleName(), "getObj method" );
             return main.get(z);
         }
         else
         {
             logger.exiting( getClass().getSimpleName(), "getObj method - object doesnt exist" );
             throw new ObjectNotFound();
         }
             
             
         
      }
     
      
      //*******************************************************************
      /**
       * 
       * @param id 
       * @return if element with specified id exist returns its position
       * else returns -1
       */
      public int ifExist(int id){
          
          logger.entering(getClass().getSimpleName(), "ifExist method");
          for (T i : main){
             if( i.getId() == id ) {
                 logger.exiting( getClass().getSimpleName(), "ifExist method - object exist" );
                 return main.indexOf(i);
             }
             
          }
          logger.exiting( getClass().getSimpleName(), "ifExist method - object doesnt exist" );
          return -1;
          
      }
      
      
      //*******************************************************************
      public void show(){
          for (T i : main) System.out.println(i);
      }
      
      //*******************************************************************
      /**
       * Function deletes item with specified id or throws exception if 
       * object with specified id does not exist
       * @param id
       * @throws ObjectNotFound 
       */
      public void delete(int id) throws ObjectNotFound {
          
          logger.entering(getClass().getSimpleName(), "delete method");
          int z = ifExist(id);
          if ( z>=0 ) {
              logger.log( Level.SEVERE, "delete method : size of list before delete {0}", main.size() );
              main.remove(id);
              logger.log( Level.SEVERE, "delete method : size of list after delete {0}", main.size() );
              logger.exiting( getClass().getSimpleName(), "delete method - deleted successfully" );
          }
          else{
              logger.exiting( getClass().getSimpleName(), "delete method - object doesnt exist" );
              throw new ObjectNotFound();
          }
              
              
      }
      
      //*******************************************************************
      public abstract void sort();
      
    
}
